package ;
import easelhx.display.Container;
import easelhx.display.Graphics;
import easelhx.display.Shape;
import easelhx.display.Text;
import easelhx.utils.Ticker;

/**
 * ...
 * @author 
 */

class CacheVdayExample extends Example
{
	private var container:Container;
	private var captureContainers:Array<Container>;
	private var captureIndex:Int;

	public function new() 
	{
		super();
		
		var w:Int = canvas.width;
		var h:Int = canvas.height;

		container = new Container();
		stage.addChild(container);

		captureContainers = new Array<Container>();
		captureIndex = 0;

		// create a large number of slightly complex vector shapes, and give them random positions and velocities:
		for (i in 0...50) {
			var heart = new Shape();
			heart.graphics.beginFill(Graphics.getHSL(Math.random()*30-45, 100, 50+Math.random()*30));
			heart.graphics.moveTo(0,-9).curveTo(0,-20,8,-20).curveTo(16,-20,16,-10).curveTo(16,0,0,12);
			heart.graphics.curveTo(-16,0,-16,-10).curveTo(-16,-20,-8,-20).curveTo(0,-20,0,-9);
			heart.y = -100;

			container.addChild(heart);
		}

		var text = new Text("the longer I'm with you\nthe more I love you", "bold 24px Arial", "#312");
		text.textAlign = "center";
		text.x = w/2;
		text.y = h/2;
		stage.addChild(text);

		for (i in 0...100) {
			var captureContainer = new Container();
			captureContainer.cache(0,0,w,h);
			captureContainers.push(captureContainer);
		}

		// start the tick and point it at the window so we can do some work before updating the stage:
		Ticker.addListener(this);
		Ticker.setFPS(30);
	}
	
	override public function run():Void
	{
		super.run();
	}
	
	private function tick() : Void
	{
		var w:Int = canvas.width;
		var h:Int = canvas.height;
		var l:Int = container.getNumChildren();

		captureIndex = (captureIndex+1)%captureContainers.length;
		stage.removeChildAt(0);
		var captureContainer = captureContainers[captureIndex];
		stage.addChildAt(captureContainer,0);
		captureContainer.addChild(container);

		// iterate through all the children and move them according to their velocity:
		for (i in 0...l) {
			var heart = container.getChildAt(i);
			if (heart.y < -50) {
				Reflect.setProperty(heart, "_x", Math.random() * w);
				heart.y = h*(1+Math.random())+50;
				Reflect.setProperty(heart, "perX", (1+Math.random())*h);
				Reflect.setProperty(heart, "offX", Math.random()*h);
				Reflect.setProperty(heart, "ampX", Reflect.getProperty(heart, "perX") * 0.1 * (0.2 + Math.random()));
				Reflect.setProperty(heart, "velY", -Math.random() * 2 - 2);
				heart.scaleX = heart.scaleY = Math.random()+1;
				heart.rotation = Math.random()*40-20;
				heart.alpha = Math.random();
			}
			heart.y += Reflect.getProperty(heart, "velY");
			heart.x = Reflect.getProperty(heart, "_x") + Math.cos((Reflect.getProperty(heart, "offX") + heart.y) / Reflect.getProperty(heart, "perX") * Math.PI * 2) * Reflect.getProperty(heart, "ampX");
		}

		captureContainer.updateCache("source-over");

		// draw the updates to stage:
		stage.update();
	}
	
}